<?php

try {
    ini_set('display_errors', 'On');
    header('Content-Type: application/json;charset=UTF-8');
    
    $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
    
    if ($origin) {
        header('Access-Control-Allow-Origin:' . $origin);
        header('Access-Control-Allow-methods", "POST, PUT, GET, OPTIONS, DELETE');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With,Authorization,Content-Type,token,Access-Token');
    } else {
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-methods", "POST, PUT, GET, OPTIONS, DELETE');
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Headers: X-Requested-With,X_Requested_With,Authorization,Content-Type,token,Access-Token');
    }
    
    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
        exit;
    }
    
    error_reporting(E_ALL);
    define('ROOT', dirname(__DIR__));
    include __DIR__ . '/../bootstrap/env.php';
    
    /** @var \application\units\Di $di */
    $di = include __DIR__ . '/../bootstrap/bootstrap.php';
    
    if (!isset($_GET['method'])) {
        throw new LogicException('Params method Must Required', 500);
    }
    
    $method = $_GET['method'];
    if (!$method) {
        throw new LogicException('Params method Not Empty', 500);
    }
    
    $router = $di->router;
    
    if (!isset($router[$method])) {
        throw new RuntimeException(sprintf('method %s Not Found', $method), 404);
    }
    
    $controller = $router[$method];
    
    if (is_array($controller)) {
        ['path' => $controller] = $controller;
    }
    
    if (!is_file($controller)) {
        throw new RuntimeException(sprintf('method %s Not Found', $method), 500);
    }
    
    // 权限验证
    include __DIR__ . '/../bootstrap/permission.php';
    
    $data = include $controller;
    echo json_encode([
        'code'    => 0,
        'msg'     => 'success',
        'data'    => $data,
        'desc'    => '请求成功',
        'runtime' => sprintf('%.2fms', (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000),
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (\LogicException $e) {
    echo json_encode([
        'code'    => $e->getCode() ?: 1,
        'msg'     => $e->getMessage(),
        'data'    => new stdClass(),
        'desc'    => '',
        'runtime' => sprintf('%.2fms', (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000),
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (\PDOException $e) {
    http_response_code(500);
    echo json_encode([
        'code'    => 500,
        'msg'     => $e->getMessage() ?: '数据库错误',
        'data'    => $e->getTrace(),
        'sql'     => $di->db->getSql(true),
        'desc'    => $e->getCode() == 0 ? '' : (string)$e,
        'runtime' => sprintf('%.2fms', (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000),
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (\Throwable $e) {
    http_response_code(500);
    echo json_encode([
        'code'    => 500,
        'msg'     => $e->getMessage() ?: '系统错误',
        'data'    => $e->getTrace(),
        'desc'    => (string)$e,
        'runtime' => sprintf('%.2fms', (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) * 1000),
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}